package org.com.blbl.VideoQuality;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

public class VideoQualityMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    private Text type = new Text();
    private LongWritable playCount = new LongWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // Split the line by the delimiter (",")
        String[] fields = value.toString().split(",");
        if (fields.length >= 5) {
            // Extract the type (field 3) and play count (field 5)
            String typeField = fields[3].trim(); // 第三位分类名称
            String playCountField = fields[4].trim(); // 第五位播放量

            try {
                long playCountValue = Long.parseLong(playCountField);
                type.set(typeField); // Set the type
                playCount.set(playCountValue); // Set the play count
                context.write(type, playCount); // Write to context
            } catch (NumberFormatException e) {
                // Ignore lines with invalid play count values
            }
        }
    }
}
